package com.yq.homework.day29;


/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/06/29 14:27
 * 要求2个线程交替打印 1A2B3C4D
 * wait notify
 * 附加: 怎么保证某个线程先执行 比如先打印A 再打印1
 */
public class Demo01 {
    public static final Object OBJECT = new Object();
    static char a = 'A';
    public static void main(String[] args) {

        new Thread(() -> {
            synchronized (OBJECT){
                for (int i = 1; i <= 26; i++) {
                    OBJECT.notify();
                    // System.out.println(Thread.currentThread().getName()+"======="+i);
                    System.out.print(i);
                    try {
                        OBJECT.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                OBJECT.notify();
            }


        },"线程一").start();

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        new Thread(()->{
            synchronized (OBJECT){
                for (int i = 0; i < 26; i++) {
                    OBJECT.notify();
                    // System.out.println(Thread.currentThread().getName()+"======="+(char)a++);
                    System.out.print((char)a++);
                    try {
                        OBJECT.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                OBJECT.notify();
            }
        },"线程二").start();
    }
}
